

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SettingsWindow : BaseWindow
{
	private bool restoredAnything;

	[SerializeField]
	private Popup m_popup;

	[SerializeField]
	private GameObject m_vibroRightPart;

	[SerializeField]
	private GameObject m_vibroWrongPart;

	[SerializeField]
	private Switcher m_vibroRightSwitcher;

	[SerializeField]
	private Switcher m_vibroWrongSwitcher;

	[SerializeField]
	private Switcher m_soundsSwitcher;

	[SerializeField]
	private Switcher m_highlightCellsSwitcher;

	[SerializeField]
	private Switcher m_loupeSwitcher;

	[SerializeField]
	private GameObject m_removeAdsButton;

	[SerializeField]
	private Text m_groupText;

	protected override string WindowName
	{
		get
		{
			return "mainMenu_moreTab";
		}
	}

	private void OnGUI()
	{
	}

	private void Awake()
	{
		this.m_vibroRightSwitcher.UpdateState(AppData.VibroRightEnabled, true);
		this.m_vibroWrongSwitcher.UpdateState(AppData.VibroWrongEnabled, true);
		this.m_soundsSwitcher.UpdateState(AppData.SoundsEnabled, true);
		this.m_highlightCellsSwitcher.UpdateState(AppData.BulbMode, true);
		this.m_loupeSwitcher.UpdateState(AppData.LoupeEnabled, true);
		this.m_vibroRightPart.SetActive(VibroWrapper.IsVibroAvailable());
		this.m_vibroWrongPart.SetActive(VibroWrapper.IsVibroAvailable());
		this.OnPurchaseHandlerRemoveAds(false, SubscriptionType.remove_ads);
	}

	public void VibroRightButtonClick()
	{
		AppData.VibroRightEnabled = !AppData.VibroRightEnabled;
		this.m_vibroRightSwitcher.UpdateState(AppData.VibroRightEnabled, false);
		AudioManager.Instance.PlayClick();
	}

	public void VibroWrongButtonClick()
	{
		AppData.VibroWrongEnabled = !AppData.VibroWrongEnabled;
		this.m_vibroWrongSwitcher.UpdateState(AppData.VibroWrongEnabled, false);
		AudioManager.Instance.PlayClick();
	}

	public void SoundButtonClick()
	{
		AppData.SoundsEnabled = !AppData.SoundsEnabled;
		this.m_soundsSwitcher.UpdateState(AppData.SoundsEnabled, false);
		AudioManager.Instance.PlayClick();

		if (BackgroundMusic.musicMgr != null)
		{
			BackgroundMusic.musicMgr.RefreshSettings();
		}
		if (AudioManager.Instance != null)
		{
			AudioManager.Instance.RefreshSettings();
		}
	}

	public void HighlightCellsClick()
	{
		AppData.BulbMode = !AppData.BulbMode;
		this.m_highlightCellsSwitcher.UpdateState(AppData.BulbMode, false);
		AudioManager.Instance.PlayClick();
	}

	public void LoupeClick()
	{
		AppData.LoupeEnabled = !AppData.LoupeEnabled;
		this.m_loupeSwitcher.UpdateState(AppData.LoupeEnabled, false);
		AudioManager.Instance.PlayClick();
	}

	public void TutorialButtonClick()
	{
		TutorialWindow tutorialWindow = WindowManager.Instance.OpenTutorial();
		tutorialWindow.Init("settings", "2D");
		AudioManager.Instance.PlayClick();
	}

	public void Tutorial3DButtonClick()
	{
		TutorialWindow tutorialWindow = WindowManager.Instance.OpenTutorial3D();
		tutorialWindow.Init("settings", "3D");
		AudioManager.Instance.PlayClick();
	}

	public void RateUsButtonClick()
	{
		AnalyticsManager.Instance.RateUsClicked();
		RateUsTool.OpenRateUs();
		AudioManager.Instance.PlayClick();
	}

	public void HelpButtonClick()
	{
		AudioManager.Instance.PlayClick();
	}

	public void SupportButtonClick()
	{
		AudioManager.Instance.PlayClick();
	}

	public void ShareAppButtonClick()
	{

	}

	public void TermsButtonClick()
	{
		Application.OpenURL(AppPathsConfig.TermsUrl);
	}

	public void PrivacyButtonClick()
	{
		Application.OpenURL(AppPathsConfig.PrivacyUrl);
	}

	public void MoreAppsButtonClick()
	{
		AnalyticsManager.Instance.MoreAppsClicked();
		Application.OpenURL("https://play.google.com/store/apps/dev?id=6817085628932553524");
	}

	public void RemoveAdsClick()
	{
		
	}

	private void OnPurchaseHandlerRemoveAds(bool res, SubscriptionType subscrType)
	{
		
		this.m_removeAdsButton.SetActive(false);
	}

	public void RestoreButtonClick()
	{
		base.StartCoroutine(this.WaitForRestored());
	
		AnalyticsManager.Instance.RestoreButtonClicked();
		AudioManager.Instance.PlayClick();
	}

	private void OnPurchaseHandler(bool res, SubscriptionType type)
	{
		this.restoredAnything = true;
		
	}
	public void ChangeGroupButtonClick()
	{

		this.UpdateGroupText();
		this.OnPurchaseHandlerRemoveAds(false, SubscriptionType.remove_ads);
	}

	private void UpdateGroupText()
	{
		
	}

	public void CloseButtonClick()
	{
		WindowManager.Instance.CloseMe(this);
		AnalyticsManager.Instance.BackButtonClicked();
		AudioManager.Instance.PlayClick();
	}

	private void OnEnable()
	{
		this.UpdateGroupText();
	}

	private void OnDestroy()
	{
		//IAPWrapper instance = IAPWrapper.Instance;
		//instance.OnPurchase = (Action<bool, SubscriptionType>)Delegate.Remove(instance.OnPurchase, new Action<bool, SubscriptionType>(this.OnPurchaseHandlerRemoveAds));
	}
	private IEnumerator WaitForRestored()
	{
		var timer = 2f;
		while (true)
		{
			if (timer > 0f)
			{
				timer -= Time.deltaTime;
				yield return null;
				continue;
			}
			if (this.restoredAnything)
			{
			
					AnalyticsManager.Instance.PurchasesRestored();
					this.m_popup.Show(LocalizationManager.Instance.GetString("restored1"));
				//}
				//else
				//{
				//	this.m_popup.Show(LocalizationManager.Instance.GetString("restored2"));
				//}
			}
			else
			{
				this.m_popup.Show(LocalizationManager.Instance.GetString("restored0"));
			}
			yield break;
		}
	}
}
